package it.slkfarm.gps;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;

public class GpsTracker extends Service implements LocationListener
{
	/**
	 * @uml.property  name="context"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Context context;
	/**
	 * @uml.property  name="isGPSEnabled"
	 */
	boolean isGPSEnabled = false;
	/**
	 * @uml.property  name="isNetworkEnabled"
	 */
	boolean isNetworkEnabled = false;
	/**
	 * @uml.property  name="providerEnabled"
	 */
	boolean providerEnabled;
	/**
	 * @uml.property  name="canGetLocation"
	 */
	boolean canGetLocation = false;
	/**
	 * @uml.property  name="location"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	Location location; 
	/**
	 * @uml.property  name="latitude"
	 */
	double latitude;
	/**
	 * @uml.property  name="longitude"
	 */
	double longitude;
	/**
	 * @uml.property  name="altitude"
	 */
	double altitude;
	private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
	private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
	/**
	 * @uml.property  name="locationManager"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	protected LocationManager locationManager;
	
	public GpsTracker(Context c)
	{
		this.context = c;
		getLocation();
	}
	
	/**
	 * @return
	 * @uml.property  name="location"
	 */
	public Location getLocation() 
	{
		locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
		isGPSEnabled = locationManager.isProviderEnabled
				(LocationManager.GPS_PROVIDER);
		isNetworkEnabled = locationManager.isProviderEnabled
				(LocationManager.NETWORK_PROVIDER);
		if (!isGPSEnabled && !isNetworkEnabled)
		{
			Log.i("gpstracker","disabled");
			this.providerEnabled = false;
		}
		else
		{
			this.providerEnabled = true;
			this.canGetLocation = true;
			Log.i("getLoc", "si");
			if (isNetworkEnabled)
			{
				locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
						MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
				Log.i("gpstracker","network");
				if (locationManager != null)
				{
					location = locationManager.
							getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
				}
			}
			if (isGPSEnabled)
			{
				locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
						MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
				Log.i("gpstracker","gps");
				if (locationManager != null)
				{
					location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				}
			}
		}
		return location;		
	}

	public void stopUsingGps()
	{
		if (locationManager != null)
			locationManager.removeUpdates(GpsTracker.this);
	}
	
	public boolean canGetLocation()
	{
		return canGetLocation;
	}
	
	/**
	 * @return
	 * @uml.property  name="providerEnabled"
	 */
	public boolean isProviderEnabled()
	{
		return providerEnabled;
	}
	
	public Location returnLocation()
	{
		return location;
	}
	
	public void onLocationChanged(Location arg0) 
	{
		// TODO Auto-generated method stub
		
	}

	public void onProviderDisabled(String arg0) 
	{
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String arg0) 
	{
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public IBinder onBind(Intent arg0) 
	{
		// TODO Auto-generated method stub
		return null;
	}

}
